﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.Statistics;
using MINERP.MODEL.FoundationModel;
using MINERP.COM;
using DevExpress.XtraEditors.Controls;
using MINERP.ComControls.Extension;

namespace MINERP.Statistics.Forms
{
    public partial class FrmNewPerformance2 : Form
    {
        private NewPerformanceBLL _bll;
        private PerformanceBLL _oldBll;
        private FrmNewPerformance2()
        {
            InitializeComponent();
            _bll = new NewPerformanceBLL();
            _oldBll = new PerformanceBLL();
        }

        private static FrmNewPerformance2 _instance;

        public static FrmNewPerformance2 GetInstance
        {
            get
            {
                if (_instance == null || _instance.Disposing)
                {
                    _instance = new FrmNewPerformance2();
                }

                return _instance;
            }
        }


        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (dtStart.DateTime > dtEnd.DateTime)
            {
                MessageBox.Show("起始日期必须小于结束日期");
                dtStart.DateTime = dtEnd.DateTime.Date;
                this.Cursor = Cursors.Default;
                return;
            }

            List<int> selectedUserIDList = new List<int>();
            foreach (CheckedListBoxItem item in cmkUser.Properties.Items)
            {
                if (item.CheckState == CheckState.Checked)
                {
                    selectedUserIDList.Add((int)item.Value);
                }
            }

            if (selectedUserIDList.Count < 1)
            {
                MessageBox.Show("请选择要查询的员工!");
                this.Cursor = Cursors.Default;
                return;
            }
            if (!checkBoxBackUp.Checked)
            {
                DataTable table = _bll.GetPerfromances(dtStart.DateTime, dtEnd.DateTime, selectedUserIDList);
                gcEmployeePerformance.DataSource = table;
                gcEmployeePerformance.RefreshDataSource();
            }
            else
            {
                DataTable table = _bll.GetPerfromancesBK(dtStart.DateTime, dtEnd.DateTime, selectedUserIDList);
                gcEmployeePerformance.DataSource = table;
                gcEmployeePerformance.RefreshDataSource();
            }
           
            this.Cursor = Cursors.Default;
        }

        private List<NewPerformanceInfo> _resultList;//查询出来的所有结果集
        private List<ViewPerformanceInfo> _ListMainView;
        void BindMainView()
        {
            if (_ListMainView == null)
            {
                _ListMainView = new List<ViewPerformanceInfo>();
            }
            else
            {
                _ListMainView.Clear();
            }
            //if (_ListMainView.Count < 1)
            //{
            //以每一个人为组分别显示
            foreach (var ti in _resultList.GroupBy(t => t.UserName))
            {
                _ListMainView.Add(new ViewPerformanceInfo()
                    {
                        UserName = ti.Key,
                        TechName = ti.FirstOrDefault().TechName,
                        PutInTotalNum = ti.Sum(t => t.PhotoTotalNum),
                        OverTotalNum = ti.Where(t => t.Status == (int)OrderInfoStatus.Completed).Sum(t => t.PhotoTotalNum),
                        //OverTotalNum = ti.Where(t => t.Status == (int)OrderInfoStatus.Completed).Sum(t => t.ProductPhotoNum),
                        OverAreas = ti.Sum(t => t.OrderArea),//打单时实际的面积
                        PutInAreas = ti.Sum(t => t.OrderArea) + ti.Where(t => t.Status != (int)OrderInfoStatus.Completed).Sum(t => t.PreOrderArea),
                        PutInMultileNum = ti.Sum(t => t.MultiplePageNum),
                        OverMultileNum = ti.Where(t => t.Status == (int)OrderInfoStatus.Completed).Sum(t => t.MultiplePageNum),
                        PutInSingleNum = ti.Sum(t => t.SinglePageNum),
                        OverSingleNum = ti.Where(t => t.Status == (int)OrderInfoStatus.Completed).Sum(t => t.SinglePageNum),
                        OverTotalCount = ti.Where(t => t.Status == (int)OrderInfoStatus.Completed).Sum(t => t.OrderCount),
                        ProductAllPhotoNum = ti.Where(t => t.Status == (int)OrderInfoStatus.Completed).Sum(t => t.ProductPhotoNum)//结单产品中照片的总数量
                    });
            }
            //}

            //绑定显示
            gcEmployeePerformance.MainView = gridView1;
            gcEmployeePerformance.DataSource = _ListMainView;
            gcEmployeePerformance.RefreshDataSource();
        }

        /// <summary>
        /// 用来显示主界面的模型
        /// </summary>
        class ViewPerformanceInfo
        {
            public int UserId { get; set; }
            public string UserName { get; set; }
            public string TechName { get; set; }
            public int PutInSingleNum { get; set; }
            public int OverSingleNum { get; set; }
            public int PutInMultileNum { get; set; }
            public int OverMultileNum { get; set; }
            public double PutInAreas { get; set; }
            public double OverAreas { get; set; }
            public int PutInTotalNum { get; set; }//提交照片张数
            public int OverTotalNum { get; set; }//结单面照片张数
            public decimal OverTotalCount { get; set; }//结单金额
            public int ProductAllPhotoNum { get; set; }//产品里最终包含照片总数
        }


        private void FrmLookSelftPerformance_FormClosing(object sender, FormClosingEventArgs e)
        {
            _instance = null;
        }

        private void FrmLookSelftPerformance_Load(object sender, EventArgs e)
        {
            BandingGroup(_oldBll.GetAllGroup());
            dtStart.DateTime = DateTime.Now.Date;
            dtEnd.DateTime = DateTime.Now.Date;
        }

        private void cmbGroup_SelectionChangeCommitted(object sender, EventArgs e)
        {
            int GroupId = ((Tb_WorkerGroup_Info)this.cmbGroup.SelectedItem).GroupId;
            BindWorkerList(GroupId);
        }

        private void BindWorkerList(int GroupId)
        {
            List<UserInfo> userList = new List<UserInfo>();
            if (GroupId > 0)
            {
                userList = _bll.GetUserListByGroupId(GroupId);
            }
            cmkUser.Properties.DataSource = userList;
            cmkUser.Properties.DisplayMember = "UserName";
            cmkUser.Properties.ValueMember = "UserID";
        }

        void BandingGroup(List<Tb_WorkerGroup_Info> list)
        {
            cmbGroup.DisplayMember = "GroupName";
            cmbGroup.ValueMember = "GroupId";
            cmbGroup.DataSource = list;
        }

        private void repositoryItemHyperLinkEdit1_Click(object sender, EventArgs e)
        {
            //显示明细
            gcEmployeePerformance.DataSource = _resultList.Where(t => t.UserName ==
                ((ViewPerformanceInfo)gridView1.GetFocusedRow()).UserName).ToList();
            gcEmployeePerformance.MainView = gridView2;
            gcEmployeePerformance.Refresh();
            btnBack.Enabled = true;

        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            gcEmployeePerformance.DataSource = _ListMainView;
            gcEmployeePerformance.MainView = gridView1;
            gcEmployeePerformance.Refresh();
            btnBack.Enabled = false;
        }

        private void gridView2_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                int status = ((NewPerformanceInfo)gridView2.GetRow(e.RowHandle)).Status;
                if (status == (int)OrderInfoStatus.Deleted)
                    e.Appearance.BackColor = Color.Red;//被删除的
                else if (status == (int)OrderInfoStatus.Doing || status == (int)OrderInfoStatus.Completed)
                {
                    e.Appearance.BackColor = Color.YellowGreen;
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            //{
            //    //saveFileDialog1.FileName = cmbGroup.Text+"组的员工业绩.xls";

            //    string saveDir = saveFileDialog1.FileName;
            //    if (!string.IsNullOrEmpty(saveDir))
            //    {
            //        gridView1.ExportToXls(saveDir);
            //        MessageBox.Show("导出成功!");
            //    }
            //}
            //DevGridViewExport.ExportToExcel("人员业绩" + DateTime.Now.ToString("yyyyMMddHHmmss"), (DevExpress.XtraGrid.Views.Grid.GridView)gcEmployeePerformance.MainView);
            string fileName="人员业绩" + DateTime.Now.ToString("yyMMddHHmm");
            DevControlExtension.ExportToExcel(gcEmployeePerformance, this, fileName);
        }
    }
}
